<?php

class IndexModel extends Model{
	
	protected $lists=array();
	
	/* 获取所有栏目信息 */
	public function getList(){
		$sql='select * from category order by cat_id asc';
		return $this->db->getAll($sql);
	}
	
	/* 获取所有栏目并排序 */
	public function catSort($arr,$parent_id=0,$lev=1){
		$this->lists=array();
		$this->catSort2($arr,$parent_id,$lev);
		return $this->lists;
	}
	
	/* 给栏目排序 */
	public function catSort2($arr,$parent_id=0,$lev=1){
		foreach($arr as $v){
			if($v['parent_id']==$parent_id){
				$v['lev']=$lev;
				$this->lists[]=$v;
				$this->catSort2($arr,$v['cat_id'],$lev+1);
			}
			
		}
	}
	
	/* 删除一行栏目 */
	public function del($cat_id){
		$sql='delete from category where cat_id='.$cat_id;
		if($this->db->query($sql)){
			return $this->db->affected_rows();
		}
		return false;
	}
	
	/* 获取某一栏目下所有子栏目 */
	public function getSon($cat_id){
		$sql='select * from category where parent_id='.$cat_id;
		return $this->db->getAll($sql);
	}
	
	/* 获取某一栏目所有信息 */
	public function info($cat_id){
		$sql='select * from category where cat_id='.$cat_id;
		return $this->db->getRow($sql);
	}
	
	/* 修改某一栏目的信息 */
	public function up($data,$cat_id=-1){
		$where='cat_id='.$cat_id;
		return $this->db->autoExecute($data,'category','update',$where);
	}
	
	/* 找父类树 */
	public function getTree($list,$cat_id){
		static $arr=array();
		foreach($list as $v){
			if($v['cat_id']==$cat_id){
				$this->getTree($list,$v['parent_id']);
				$arr[]=$v;
			}
		}
		return $arr;
	}
	
	/* 添加一行商品并返回商品id */
	public function add($data){
		if($this->db->autoExecute($data,'goods')){
			return $this->db->last_id();
		}
		return false;
	}
	
	/* 修改一行商品 */
	public function update($data,$where){
		if($this->db->autoExecute($data,'goods','update',$where)){
			return true;
		}
		return false;
	}
	
	/* 获取商品的总行数 */
	public function getRowCount(){
		$sql = 'select count(*) from goods';
		return $this->db->getOne($sql);
	}
	
	/* 交易失败，回滚 */
	public function rollback($goods_id){
		$sql = 'update goods set is_delete=1 where goods_id='.$goods_id;
		if($this->db->query($sql)){
			return $this->db->affected_rows();
		}
		return false;
	}
	
	/* 删除一行商品 */
	public function delete($goods_id){
		$sql = "delete from goods where goods_id = $goods_id";
		return $this->db->query($sql);
	}
	
	/* 获取最热或最新或最好的商品 */
	public function getTop($type=''){
		if($type!='best' && $type!='hot' && $type!='new'){
			return false;
		}
		$sql = 'select goods_id,goods_name,market_price,shop_price,goods_thumb from goods where is_delete=0 and is_'.$type.'=1 limit 4';
		return $this->db->getAll($sql);
	}
	
	/* 获取一行商品 */
	public function detail($goods_id){
		$sql = 'select * from goods where is_delete=0 and goods_id='.$goods_id;
		return $this->db->getRow($sql);
	}
	
	/* 获取某些栏目下的商品信息 */
	public function catGoods($arr){
		$in='('.implode(',',$arr).')';
		
		$sql = 'select goods_id,mername,goods_name,goods_thumb,shop_price,market_price from goods left join mer on goods.mer_id=mer.mer_id where cat_id in '.$in.' limit 6';
	
		return $this->db->getAll($sql);
	}
	
	/* 商品盒查询商品 */
	public function goodsbox($cat_id, $property){
		if($cat_id != 0){
			$sql = 'select goods_id, goods_name, goods_thumb, market_price, shop_price from goods where is_' . $property . ' = 1 and cat_id = ' . $cat_id . ' order by goods_id desc limit 0,4';
		}else{
			$sql = 'select goods_id,goods_name, goods_thumb, market_price, shop_price from goods where is_' . $property . ' order by goods_id desc limit 0,4';
		}
		return $this->db->getAll($sql);
	}
	
	/* 根据关键字查询商品 */
	public function goodsSearch($keywords, $pageNow, $pageSize){
		$sql = 'select goods_id, goods_name, market_price, shop_price, goods_thumb from goods where keywords like ' . "'%" . $keywords ."%' limit " . (($pageNow-1)*$pageSize) . ',' . $pageSize;
		return $this->db->getAll($sql);
	}
	
	/* 根据关键字查询商品并排序 */
	public function get_goods_sort($keywords, $sort, $pageNow, $pageSize){
		$sql = 'select goods_id, goods_name, goods_thumb, market_price, shop_price from goods where keywords like ' . "'%" . $keywords ."%' order by " . $sort[0] . ' ' . $sort[1] . ' limit ' . (($pageNow-1)*$pageSize) . ',' . $pageSize;
		return $this->db->getAll($sql);
	}
	
	/* 获取某关键字下商品总行数 */
	public function getRowCount2($keywords){
		$sql = 'select count(*) from goods where keywords like ' . "'%" . $keywords . "%'";
		return $this->db->getOne($sql);
	}
	
}








